home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0048_Re: Clipboard and Streams.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  2.3 KB  |  84 lines

  1.  
  2. In article <684156579wnr@spudsoft.demon.co.uk>,
  3.    "D:DEMONSPOOLMAIL" <jt@spudsoft.demon.co.uk> wrote:
  4. >I want to use the clipboard to store data in a proprietry format but I 
  5. >would like to just write a single set of routines to input and output 
  6. >from streams.
  7. >
  8. >Is it possible to set up a TMemoryStream object, fill it with the data 
  9. >and then give it to the clipboard?
  10. >If so, how?
  11.  
  12. Not only is it possible, but this is exactly how Borland implemented
  13. the Clipboard.GetComponent and Clipboard.SetComponent functions.
  14. Basically, you would need to register your own clipboard format
  15. with a call to RegisterClipboardFormat():
  16.  
  17. CF_MYFORMAT := RegisterClipboardFormat('My Format Description');
  18.  
  19. You would then follow these steps:
  20. 1. Create a memory stream & write your data to it.
  21. 2. Create a global memory buffer and copy the stream into it.
  22. 3. Call Clipboard.SetAsHandle() to place it on the clipboard.
  23.  
  24. Example:
  25.  
  26. var
  27.   hbuf    : THandle;
  28.   bufptr  : Pointer;
  29.   mstream : TMemoryStream;
  30. begin
  31.   mstream := TMemoryStream.Create;
  32.   try
  33.     {-- Write your data to the stream. --}
  34.     hbuf := GlobalAlloc(GMEM_MOVEABLE, mstream.size);
  35.     try
  36.       bufptr := GlobalLock(hbuf);
  37.       try
  38.         Move(mstream.Memory^, bufptr^, mstream.size);
  39.         Clipboard.SetAsHandle(CF_MYFORMAT, hbuf);
  40.       finally
  41.         GlobalUnlock(hbuf);
  42.       end;
  43.     except
  44.       GlobalFree(hbuf);
  45.       raise;
  46.     end;
  47.   finally
  48.     mstream.Free;
  49.   end;
  50. end;
  51.  
  52. IMPORTANT: Do not delete the buffer you GlobalAlloc().  Once you
  53. put it on the clipboard, it's up to the clipboard to dispose of
  54. it.  When retrieving it, again, do not delete the buffer you
  55. retrieve -- just make a copy of the contents.
  56.  
  57. To retrieve the stream and its data, do something like this:
  58.  
  59. var
  60.   hbuf    : THandle;
  61.   bufptr  : Pointer;
  62.   mstream : TMemoryStream;
  63. begin
  64.   hbuf := Clipboard.GetAsHandle(CF_MYFORMAT);
  65.   if hbuf <> 0 then begin
  66.     bufptr := GlobalLock(hbuf);
  67.     if bufptr <> nil then begin
  68.       try
  69.         mstream := TMemoryStream.Create;
  70.         try
  71.           mstream.WriteBuffer(bufptr^, GlobalSize(hbuf));
  72.           mstream.Position := 0;
  73.           {-- Read your data from the stream. --}
  74.         finally
  75.           mstream.Free;
  76.         end;
  77.       finally
  78.         GlobalUnlock(hbuf);
  79.       end;
  80.     end;
  81.   end;
  82. end;
  83.  
  84.